<?php 
$docRoot = $_SERVER ['DOCUMENT_ROOT'] . '/' . strtok ( $_SERVER ['PHP_SELF'], '/' );
include_once ($docRoot . "/application/service/feed/loader/FeedLoader.php");
include_once ($docRoot . "/application/service/db/CountryDAO.php");
?>

<?php

class CountryLoaderImpl implements FeedLoader {
	
	private $countryFeedUrl;
	private $standingsFeedUrl;
	private $sheduleFeedUrl;
	
	private $countryFeedXmlName;
	private $standingsFeedXmlName;
	private $sheduleFeedXmlName;
		
	private $countryFeedLocalPath;
	
	
	/**
	 * @param field_type $countryFeedXmlName
	 */
	public function setCountryFeedXmlName($countryFeedXmlName) {
		$this->countryFeedXmlName = $countryFeedXmlName;
	}

	/**
	 * @param field_type $standingsFeedXmlName
	 */
	public function setStandingsFeedXmlName($standingsFeedXmlName) {
		$this->standingsFeedXmlName = $standingsFeedXmlName;
	}

	/**
	 * @param field_type $sheduleFeedXmlName
	 */
	public function setSheduleFeedXmlName($sheduleFeedXmlName) {
		$this->sheduleFeedXmlName = $sheduleFeedXmlName;
	}

	/**
	 * @param field_type $countryFeedLocalPath
	 */
	public function setCountryFeedLocalPath($countryFeedLocalPath) {
		$this->countryFeedLocalPath = $countryFeedLocalPath;
	}

	/**
	 * @param field_type $countryFeedUrl
	 */
	public function setCountryFeedUrl($countryFeedUrl) {
		$this->countryFeedUrl = $countryFeedUrl;
	}

	/**
	 * @param field_type $standingsFeedUrl
	 */
	public function setStandingsFeedUrl($standingsFeedUrl) {
		$this->standingsFeedUrl = $standingsFeedUrl;
	}

	/**
	 * @param field_type $sheduleFeedUrl
	 */
	public function setSheduleFeedUrl($sheduleFeedUrl) {
		$this->sheduleFeedUrl = $sheduleFeedUrl;
	}

	/* (non-PHPdoc)
	 * @see FeedLoader::loadFeed()
	 */
	public function loadFeed() {
		// TODO Auto-generated method stub
		$countryFeedContent = file_get_contents ( $this->countryFeedUrl );
		$countryStandingsFeedContent = file_get_contents ( $this->standingsFeedUrl );
		$countrySheduleFeedContent = file_get_contents ( $this->sheduleFeedUrl );
		
		$tournamentXmlObject = simplexml_load_string ( $countryFeedContent );
		
		$attributeId = "file_group";
		$attributeName = "country";
		
		$countryId = $tournamentXmlObject->children()->attributes()->$attributeId;
		$countryName = $tournamentXmlObject->children()->attributes()->$attributeName;

		mkdir ( $this->countryFeedLocalPath . $countryId, 0700 );
		
		$countryFeedSaveFullPath = $this->countryFeedLocalPath . $countryId . $this->countryFeedXmlName;
		$countryStandingsFeedSaveFullPath = $this->countryFeedLocalPath . $countryId . $this->standingsFeedXmlName;
		$countrySheduleFeedSaveFullPath = $this->countryFeedLocalPath . $countryId . $this->sheduleFeedXmlName;
		
		$fh = fopen ( $countryFeedSaveFullPath, 'w' );
		fwrite ( $fh, $countryFeedContent );
		fclose ( $fh );
		
		$fh = fopen ( $countryStandingsFeedSaveFullPath, 'w' );
		fwrite ( $fh, $countryStandingsFeedContent );
		fclose ( $fh );
		
		$fh = fopen ( $countrySheduleFeedSaveFullPath, 'w' );
		fwrite ( $fh, $countrySheduleFeedContent );
		fclose ( $fh );
		
	     $countryDAO = new CountryDAO();
	     $countryDAO->addCountry($countryId, $countryName, $this->countryFeedUrl, $this->standingsFeedUrl, $this->sheduleFeedUrl);
		
	}


}

?>